
-- by Chris Mackey

--pieces
--body
local head = piece "head"
local neck = piece "neck"
local torso = piece "torso"
local pelvis = piece "pelvis"
local kilt = piece "kilt"
local beard = piece "beard"
local hat = piece "hat"

--left side
local l_arm = piece "l_arm"
local l_fore = piece "l_fore"
local l_hand = piece "l_hand"
local l_foot = piece "l_foot"

--right side
local r_arm = piece "r_arm"
local r_fore = piece "r_fore"
local r_hand = piece "r_hand"
local r_foot = piece "r_foot"

--weapons
local wand = piece "wand"
local mk_staff = piece "mk_staff"
local staff = piece "staff"

local scimitar = piece "scimitar"
local claymore = piece "claymore"
local katana = piece "katana"

local potion = piece "potion"
local book_spine = piece "book_spine"
local book_lcov = piece "book_lcov"
local book_rcov = piece "book_rcov"

--local speed = (Spring.UnitScript.GetUnitValue( COB.CURRENT_SPEED ))/20

--signals
local walk = 2
local aim  = 4

function script.Create()
	Hide( wand )
	Hide( mk_staff )
	--Hide( staff )
	Hide( potion )
	Hide( scimitar )
	Hide( claymore )
	--Hide( katana )
	Hide( book_spine )
	Hide( book_lcov )
	Hide( book_rcov )
end

local function Attack()
	Turn( torso, y_axis, 1, 9 )
	Turn( head, y_axis, -1, 9 )
	
	Turn( r_arm, x_axis, -1.5, 6 )
	Turn( r_arm, y_axis, .3, 6 )
	Turn( r_fore, x_axis, 0, 6 )
	Turn( r_fore, y_axis, -3, 9 )
	Turn( r_hand, x_axis, 1, 6 )
	Turn( r_hand, y_axis, 1, 6 )
	
	Turn( l_arm, x_axis, 0, 6 )
	Turn( l_arm, y_axis, 1, 5 )
	Turn( l_arm, z_axis, 1, 6 )
	Turn( l_fore, x_axis, -0.5, 6 )
	Turn( l_fore, z_axis, 0, 6 )
	
	Move( pelvis, y_axis, -2, 20 )
	Move( pelvis, z_axis, 10, 50 )
	
	Turn( kilt, y_axis, .7, 9 )
end

local function Ready()
	Turn( torso, y_axis, -1, 1.7 )
	Turn( head, y_axis, 1, 2 )
	
	Turn( r_arm, x_axis, -2, 3 )
	Turn( r_arm, y_axis, -1.5, 3 )
	Turn( r_fore, x_axis, -1.7, 1.5 )
	Turn( r_fore, y_axis, 0, 3 )
	Turn( r_hand, x_axis, 0, 3 )
	Turn( r_hand, y_axis, -1, 2 )
	
	Turn( l_arm, x_axis, -1.5, 2 )
	Turn( l_arm, y_axis, 1.1, 2 )
	Turn( l_arm, z_axis, 0, 2 )
	Turn( l_fore, x_axis, 0, 1 )
	Turn( l_fore, z_axis, -1.5, 1 )
	
	Move( pelvis, y_axis, 0, 10 )
	Move( pelvis, z_axis, 0, 20 )
	
	Turn( kilt, x_axis, 0, 1 )
	Turn( kilt, y_axis, 0, 1 )
	Turn( kilt, z_axis, 0, 1 )
	
end

local function WizWalk()
	SetSignalMask( walk )
	while( true ) do 
		Turn( torso, y_axis, -.25, 1 )
		Turn( head, y_axis, .25, 1 )
		Turn( kilt, y_axis, .5, 2.8 )
		Turn( l_foot, y_axis, -.5, 2.8 )
		Turn( r_foot, y_axis, -.5, 2.8 )
		Move( pelvis, y_axis, 2, 7 )
		
		Turn( l_arm, x_axis, .4, 2 )
		Turn( l_fore, x_axis, -.5, 2 )
		
		Turn( r_arm, x_axis, -.5, 3 )
		Turn( r_fore, x_axis, -.3, 3 )
		
		WaitForMove( pelvis, y_axis )
		Move( pelvis, y_axis, 0, 8 )
		Sleep( 250 )
		
		Turn( torso, y_axis, .25, 1 )
		Turn( head, y_axis, -.25, 1 )
		Turn( kilt, y_axis, -.5, 2.8 )
		Turn( l_foot, y_axis, .5, 2.8 )
		Turn( r_foot, y_axis, .5, 2.8 )
		Move( pelvis, y_axis, 2, 7 )
		
		Turn( l_arm, x_axis, -.5, 3 )
		Turn( l_fore, x_axis, -.3, 3 )
		
		Turn( r_arm, x_axis, .4, 2 )
		Turn( r_fore, x_axis, -.5, 2 )
		
		WaitForMove( pelvis, y_axis )
		Move( pelvis, y_axis, 0, 8 )
		Sleep( 250 )
	end
end

local function StopWalk()
	Move( pelvis, x_axis, 0, 10 )
	Move( pelvis, y_axis, 0, 10 )
	Move( pelvis, z_axis, 0, 10 )
	
	Turn( head, y_axis, 0, 2 )
	Turn( head, x_axis, 0, 2 )
	Turn( head, z_axis, 0, 2 )
	Turn( torso, x_axis, 0, 2 )
	Turn( torso, y_axis, 0, 2 )
	Turn( torso, z_axis, 0, 2 )
	Turn( kilt, x_axis, 0, 2 )
	Turn( kilt, y_axis, 0, 2 )
	Turn( kilt, z_axis, 0, 2 )
	
	Turn( l_arm, x_axis, 0, 2 )
	Turn( l_arm, y_axis, 0, 2 )
	Turn( l_arm, z_axis, 0, 2 )
	Turn( l_fore, x_axis, 0, 2 )
	Turn( l_fore, y_axis, 0, 2 )
	Turn( l_fore, z_axis, 0, 2 )
	Turn( l_hand, x_axis, 0, 2 )
	Turn( l_hand, y_axis, 0, 2 )
	Turn( l_hand, z_axis, 0, 2 )
	
	Turn( r_arm, x_axis, 0, 2 )
	Turn( r_arm, y_axis, 0, 2 )
	Turn( r_arm, z_axis, 0, 2 )
	Turn( r_fore, x_axis, 0, 2 )
	Turn( r_fore, y_axis, 0, 2 )
	Turn( r_fore, z_axis, 0, 2 )
	Turn( r_hand, x_axis, 0, 2 )
	Turn( r_hand, y_axis, 0, 2 )
	Turn( r_hand, z_axis, 0, 2 )
	
	Turn( l_foot, 1, 0, 4 )
	Turn( l_foot, 2, 0, 4 )
	Turn( l_foot, 3, 0, 1 )
	
	Turn( r_foot, 1, 0, 4 )
	Turn( r_foot, 2, 0, 4 )
	Turn( r_foot, 3, 0, 1 )
end

function script.StartMoving()
	SetSignalMask( walk )
	StartThread( StopWalk )
	StartThread( WizWalk )
end

function script.StopMoving()
	Signal( walk )
	SetSignalMask( walk )
	StartThread( StopWalk )
end

function script.QueryWeapon1() return head end

function script.AimFromWeapon1() return head end

function script.AimWeapon1( heading, pitch )
	--WaitForTurn( torso, y_axis )
	Signal( aim )
	SetSignalMask( aim )
	StartThread( Ready )
	--WaitForTurn( r_hand, y_axis )
	return true
end

function script.FireWeapon1()
	StartThread( Attack )
end
----[[
function script.QueryWeapon2() return head end

function script.AimFromWeapon2() return head end

function script.AimWeapon2( heading, pitch )
	--WaitForTurn( torso, y_axis )
	Signal( aim )
	SetSignalMask( aim )
	StartThread( Ready )
	--WaitForTurn( r_hand, y_axis )
	return true
end

function script.FireWeapon2()
	StartThread( Attack )
end
--]]
--[[
function script.QueryWeapon2() return pelvis end
function script.AimFromWeapon2() return pelvis end
function script.AimWeapon2() return true end
function script.FireWeapon2() end
--]]
function script.QueryWeapon3() return pelvis end
function script.AimFromWeapon3() return pelvis end
function script.AimWeapon3() return true end
function script.FireWeapon3() end

function script.HitByWeapon()	
	EmitSfx( torso, 1024+1 )
end

function script.Killed( damage, health )
	local severity = damage/health * 100
	
	if( severity <= 50 ) then
		EmitSfx( head, 1024+1 )
		EmitSfx( torso, 1024+1 )
		return 1 -- corpsetype
	elseif( severity <= 100 ) then
		EmitSfx( head,  1024+1 )
		EmitSfx( torso, 1024+1 )
		EmitSfx( r_arm, 1024+1 )
		EmitSfx( l_arm, 1024+1 )
		Explode( hat, SFX.FALL )
		return 1 -- corpsetype
	else
		EmitSfx( torso, 1024+2 )
		Explode( head, SFX.FALL )
		Explode( torso, SFX.FALL )
		Explode( pelvis, SFX.FALL )
		Explode( kilt, SFX.FALL )
		Explode( l_arm, SFX.FALL )
		Explode( l_fore, SFX.FALL )
		Explode( r_arm, SFX.FALL )
		Explode( r_fore, SFX.FALL )
		return 2 -- corpsetype
	end
end
